package me.yobol.greentable.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.yobol.greentable.bean.IngredientBean;
import me.yobol.greentable.service.IngredientService;

/**
 * @author Yobol
 *
 */
public class ModifyIngredientServlet extends HttpServlet {

	private static final long serialVersionUID = 7194977878221342046L;

	public ModifyIngredientServlet() {
		super();
	}
	
	public void init() throws ServletException {
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int igId = (request.getParameter("igId") == null) ? -1 : Integer.parseInt(request.getParameter("igId"));

		String redirect = null;
		if(igId > 0){
			String igName = request.getParameter("igName");
			String igDesc = request.getParameter("igDesc");
			String igImg = request.getParameter("igImg");
			float igPrice = Float.parseFloat(request.getParameter("igPrice"));
			float discPrice = igPrice;
			String igFlag = request.getParameter("igFlag");

			IngredientBean ig = new IngredientBean();
			ig.setIngredientName(igName);
			ig.setIngredientDescription(igDesc);
			ig.setIngredientImg(igImg);
			ig.setIngredientPrice(igPrice);
			ig.setIngredientDiscPrice(discPrice);
			ig.setIngredientFlag(igFlag);
			
			if(IngredientService.updateIngredient(igId, ig)){
				String msg = "modify_success";
				redirect = "/pages/ingredient/modify_ingredient.jsp?msg=" + msg + "&igId=" + igId;
			}
		}else{
			redirect = "/pages/ingredient/modify_ingredient.jsp";
		}
		
		response.sendRedirect(request.getContextPath() + redirect);
	}
	
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

}
